<template>
  <div class="map_wrapper" style="color: red;font-size: 30px;font-weight: bold;width: 100%;height: 100%;">
    <div class="map_inner" >
      <div id="my_map" style="width: 300px;height: 300px;border: 1px solid blue;">

      </div>
      <div @click="add">添加多个</div>
      <div @click="addOne">添加一个</div>
      <div @click="del">删除{{cur_index}}</div>
      <div @click="loadMap">加载地图</div>
      <div @click="delMap">--销毁地图--</div>
<!--      <img src="https://pic4.zhimg.com/v2-015d81af471588ebdf6a3a1cc6be44e7_xs.jpg?source=1940ef5c" alt="" style="width: 100px;height: 100px;">-->
<!--	  <div @click="">{{debug}}</div>-->
    </div>
  </div>
</template>

<script>
  // 设置一个图标对象

  // var allowCollision = false;
  // var layer = new AMap.LabelsLayer({
  //   zooms: [3, 20],
  //   zIndex: 1000,
  //   // collision: false,
  //   // 设置 allowCollision：true，可以让标注避让用户的标注
  //   allowCollision,
  // });
  export default {
    name: "Map",
    props: ['add_marker'],
    data(){
      return {
        cur_index:0,
        map: null,
        markers: [],
        icon: null,
        debug: 'debug'
      }
    },
    mounted() {
      this.icon = {
        // 图标类型，现阶段只支持 image 类型
        type: 'image',
        // 图片 url
        // image: 'https://a.amap.com/jsapi_demos/static/demo-center/marker/express2.png',
        image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
        // 图片尺寸
        size: [20, 20],
        // 图片相对 position 的锚点，默认为 bottom-center
        anchor: 'center',
      };
      console.debug("mountde")
    },
    methods: {
      loadMap(){
        this.map = new qq.maps.Map(document.getElementById("my_map"), {
          // 地图的中心地理坐标。
          center: new qq.maps.LatLng(39.916527,116.397128),
          zoom: 13
        });
      },//loadMap end
      addOne(){
        var _this = this
            // 图层添加到地图
            var anchor = new qq.maps.Point(10,30),
                size = new qq.maps.Size(42, 42),
                origin = new qq.maps.Point(0, 0),
                markerIcon = new qq.maps.MarkerImage(
                    // "../assets/img/logo.png",
                    "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/plane.png",
                    // "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/center.gif",
                    // "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2986340481,1038989370&fm=26&gp=0.jpg",
                    size,
                    origin,
                    anchor
                );
            size = new qq.maps.Size(60, 42)
            var shadowOrigin = new qq.maps.Point(32, 0);
            var shadow = new qq.maps.MarkerImage(
                // "../assets/img/logo.png",
                "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/plane.png",
                // "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/center.gif",
                // "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2986340481,1038989370&fm=26&gp=0.jpg",
                size,
                shadowOrigin,
                anchor
            );
            var marker = new qq.maps.Marker({
              position: this.map.getCenter(),
              map: _this.map,
              shadow: shadow,
              animation: qq.maps.MarkerAnimation.BOUNCE
            });
            marker.setIcon(markerIcon);
            this.markers.push(marker);
            (function (n) {
              console.debug("add listener")
              qq.maps.event.addListener(_this.markers[n], 'click', function() {
                console.debug("点击了标记物："+n);
                _this.cur_index = n
              });
            })(_this.markers.length-1);


      },//add end
      showInfoM(e){
        console.debug("点击标志物"+e.target.getExtData().index)
      },
      del(){
        if (this.cur_index >= 0){
          this.markers[this.cur_index].setMap(null)
          // this.markers.splice(this.cur_index, 1)
          // this.map.setFitView();
        }
      },
      add(){
        var _this = this
        for(var i = 0;i < 10; i++) {
          (function(n){
            // 图层添加到地图
            var anchor = new qq.maps.Point(10,30),
                size = new qq.maps.Size(42, 42),
                origin = new qq.maps.Point(0, 0),
                markerIcon = new qq.maps.MarkerImage(
                    // "../assets/img/logo.png",
                    "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/plane.png",
                    // "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/center.gif",
                    // "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2986340481,1038989370&fm=26&gp=0.jpg",
                    size,
                    origin,
                    anchor
                );
            size = new qq.maps.Size(60, 42)
            var shadowOrigin = new qq.maps.Point(32, 0);
            var shadow = new qq.maps.MarkerImage(
                // "../assets/img/logo.png",
                "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/plane.png",
                // "https://mapapi.qq.com/web/lbs/javascriptV2/demo/img/center.gif",
                // "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2986340481,1038989370&fm=26&gp=0.jpg",
                size,
                shadowOrigin,
                anchor
            );
            var marker = new qq.maps.Marker({
              position: new qq.maps.LatLng(39.916527+n*0.01,116.397128+n*0.01),
              map: _this.map,
              shadow: shadow,
              animation: qq.maps.MarkerAnimation.BOUNCE
            });
            marker.setIcon(markerIcon);
            _this.markers.push(marker)
            qq.maps.event.addListener(marker, 'click', function() {
              console.debug("点击了标记物："+n);
              _this.cur_index = n
            });
          })(i);
        }

      }//add end
    },
    watch: {

    }
  }
</script>

<style scoped>

</style>